package pers.cz.chat.client.conn;

import io.netty.channel.ChannelFuture;
import io.netty.util.internal.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pers.cz.chat.client.handle.ChatMessageHandle;
import pers.cz.chat.constants.JefClolorEnum;
import pers.cz.chat.filter.*;
import pers.cz.chat.utils.LogUtils;
import pers.cz.chat.utils.StringUtils;
import pers.cz.jefconfig.config.JefConfiguration;
import pers.cz.netty.client.NettyClient;
import pers.cz.netty.client.TcpNettyClient;
import pers.cz.netty.config.NettyConfigItem;
import pers.cz.netty.filter.IFilter;
import pers.cz.netty.filter.Pipeline;
import pers.cz.netty.protocal.Message;
import pers.cz.netty.protocal.MsgType;
import pers.cz.netty.util.ContextHolder;

import java.util.Scanner;

/**
 * @program: Jef-Chat
 * @description: 会话
 * @author: Cheng Zhi
 * @create: 2025-01-05 20:33
 **/
public class Session {

    private static Logger logger = LoggerFactory.getLogger(Session.class);

    public void start() throws InterruptedException {
        init();
        String host = JefConfiguration.get(NettyConfigItem.NETTY_SERVER_IP);
        logger.info("当前连接IP为：" + host);
        int port = JefConfiguration.getInt(NettyConfigItem.NETTY_SERVER_PORT, 4567);
        logger.info("当前连接端口为：" + port);

        // 接收消息过滤器
        ChatMessageHandle chatMessageHandle = new ChatMessageHandle();
        IFilter receivePipeline = new Pipeline()
                .addFirst(new LoginReceiveFilter())
                .addLast(new RegistReceiveFilter())
                .addLast(new CreateGroupReceiveFilter());
        chatMessageHandle.setFilter(receivePipeline);

        NettyClient nettyClient = new TcpNettyClient(host, port, chatMessageHandle);
        // 发送消息过滤器
        IFilter sendPipeline = new Pipeline()
                .addLast(new LoginSendFilter())
                .addLast(new HelpFilter())
                .addLast(new RegistFilter())
                .addLast(new MessageFilter())
                .addLast(new CreateGroupFilter())
                .addLast(new SwitchUserFilter())
                .addLast(new ShowUsersFilter());
        nettyClient.setFilter(sendPipeline);
        ChannelFuture connect = nettyClient.connect();
        if (connect.sync().isSuccess()) {
            while (true) {
                String content = ScannerImpl.getInput("请输入一条消息或指令：");
                if (StringUtils.isEmpty(content)) {
                    continue;
                }
                Message message = new Message();
                // 设置服务端ip和port,如果使用udp通信的时候会用到
                message.setIp(JefConfiguration.get(NettyConfigItem.NETTY_SERVER_IP));
                message.setPort(JefConfiguration.getInt(NettyConfigItem.NETTY_SERVER_PORT, 9999));
                message.setMsgType(MsgType.DATA);
                message.setContent(content);
                String userName = ContextHolder.getRequestContext().get("sourceUserName");
                if (!StringUtil.isNullOrEmpty(userName)) {
                    message.addAttachment("username", userName);
                }
                nettyClient.send(message);
            }
        }
    }

    private void init() {
        // 初始化设置日志级别
        System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "off");
    }
}
